package com.zdw.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zdw.common.constant.common.CommonConstants;
import com.zdw.demo.entity.WechatOrder;
import com.zdw.demo.entity.WechatOrderLog;
import com.zdw.demo.entity.WechatRefundHistory;
import com.zdw.demo.mapper.WechatOrderLogMapper;
import com.zdw.demo.mapper.WechatRefundHistoryMapper;
import com.zdw.demo.service.WechatRefundHistoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;


/**
 * Service : 微信支付订单 服务类
 * 用于处理实际的业务逻辑。
 */
@Service
@Slf4j
public class WechatRefundHistoryServiceImpl extends ServiceImpl<WechatRefundHistoryMapper, WechatRefundHistory> implements WechatRefundHistoryService {

    @Autowired
    private WechatRefundHistoryMapper refundHistoryMapper;

    /**
     * 新增退款记录
     *
     * @param history
     * @return
     */
    @Override
    public boolean save(WechatRefundHistory history) {
        history.setRefund_status(CommonConstants.WECHAT_REFUND_STATE_PROCESSING);
        return refundHistoryMapper.insert(history) > 0;
    }

    /**
     * 更新退款状态
     *
     * @param history
     * @return
     */
    @Override
    public boolean updateState(WechatRefundHistory history) {
        return refundHistoryMapper.updateStatus(history) > 0;
    }

    /**
     * 根据退款单号 查询订单信息
     *
     * @param outRefundNo
     * @return
     */
    @Override
    public WechatRefundHistory selectByOutRefundNo(String outRefundNo) {
        LambdaQueryWrapper<WechatRefundHistory> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(WechatRefundHistory::getOut_refund_no, outRefundNo);
        return refundHistoryMapper.selectOne(queryWrapper);
    }

    /**
     * 根据微信订单号查询所有退款记录
     *
     * @return
     */
    @Override
    public List<WechatRefundHistory> selectListByTransactionId(String transactionId) {
        LambdaQueryWrapper<WechatRefundHistory> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(WechatRefundHistory::getTransaction_id, transactionId);
        return refundHistoryMapper.selectList(queryWrapper);
    }

    /**
     * 根据内部订单号查询所有退款记录
     *
     * @return
     */
    @Override
    public List<WechatRefundHistory> selectListByOutTradeNo(String outTradeNo) {
        LambdaQueryWrapper<WechatRefundHistory> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(WechatRefundHistory::getOut_trade_no, outTradeNo);
        return refundHistoryMapper.selectList(queryWrapper);
    }

}
